package com.example.aiyou.passwordmanage.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.example.aiyou.passwordmanage.R;
import com.example.aiyou.passwordmanage.adapter.PasswordAdpater;
import com.example.aiyou.passwordmanage.adapter.RecycleViewDivider;
import com.example.aiyou.passwordmanage.adapter.SwipeItemLayout;
import com.example.aiyou.passwordmanage.base.ActivityManage;
import com.example.aiyou.passwordmanage.base.BaseActivity;
import com.example.aiyou.passwordmanage.pojo.Password;

import org.litepal.LitePal;
import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {
    private AddActivity addActivity;
    private DetailActivity detailActivity;
    private List<Password> passwords = new ArrayList<Password>();
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        initIcons();
        recyclerView = (RecyclerView) findViewById( R.id.deatil_view);
        layoutManager = new LinearLayoutManager( this);
        recyclerView.setLayoutManager( layoutManager);
        recyclerView.addItemDecoration(new RecycleViewDivider(MainActivity.this, LinearLayoutManager.HORIZONTAL));
        PasswordAdpater adapter = new PasswordAdpater(passwords,this);
        recyclerView.setAdapter(adapter);
        recyclerView.addOnItemTouchListener(new SwipeItemLayout.OnSwipeItemTouchListener(this));
        adapter.setOnItemClickListener(new PasswordAdpater.OnItemClickListener() {
           @Override
           public void onItemClick(View view, int position) {
                Password password = passwords.get(position);
               detailActivity.actionStart(MainActivity.this,password);
           }

            @Override
            public void onLongClick(View view, int position) {
                Toast.makeText(MainActivity.this,"Hello",Toast.LENGTH_SHORT).show();
            }
        });
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addActivity.actionStart(MainActivity.this,0);
            }
        });
    }
    @Override
    public void onResume(){
        super.onResume();
        initIcons();
        PasswordAdpater adapter = new PasswordAdpater(passwords,this);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new PasswordAdpater.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Password password = passwords.get(position);
                detailActivity.actionStart(MainActivity.this,password);
            }

            @Override
            public void onLongClick(View view, int position) {
                Toast.makeText(MainActivity.this,"Hello",Toast.LENGTH_SHORT).show();
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Intent intent = new Intent(MainActivity.this,SettingsActivity.class);
            startActivityForResult(intent,1);
            return true;
        }else if(id == R.id.action_about){
            Intent intent = new Intent(MainActivity.this,AboutActivity.class);
            startActivity(intent);
            return true;
        }else if(id == R.id.action_exit){
            ActivityManage.finishAll();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent){
        switch (requestCode){
            case 1:
                if(resultCode == RESULT_OK){
                    Toast.makeText(MainActivity.this,intent.getStringExtra("meg"),Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }
    public static void actionStart(Context context){
        Intent intent = new Intent(context,MainActivity.class);
        context.startActivity(intent);
    }
    public void initIcons(){
        LitePal.getDatabase();
        passwords = DataSupport.order("operattime desc").find(Password.class);
    }
}
